﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;
using Microsoft.Windows.Controls.Ribbon;
using S7.Windows.Menu;
using S7.Windows.Music;
using S7.Windows.Customers;
using S7.Windows.Feedback;
using S7.Windows.Employees;
using S7.Windows.Orders;
using S7.Windows.Reservations;
using S7.Windows.WC;
using S7.Windows.Chat;
using S7.Windows.Settings;
using S7.UserControls;
using M7;
using System.Net;

namespace S7
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : RibbonWindow
    {
        public Window1()
        {
            InitializeComponent();
            ChatServer.ChatMsgs = new List<M7.ChatClient>();
            Info.window = this;


            Thread thrTables = new Thread(new ThreadStart(() =>
            {
                try
                {
                    Table.lstTables = Table.Search();
                    wrpLiveView.Dispatcher.Invoke(new ThreadStart(() =>
                    {
                        try
                        {
                            foreach (var table in Table.lstTables)
                            {
                                var t = new TableControl();
                                t.TableNumber = table.TableID.ToString();
                                wrpLiveView.Children.Add(t);
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "Error!", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
                        }
                    }), null);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error!", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
                }
            }));
            thrTables.IsBackground = true;
            thrTables.Start();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Info.server = new TcpServer();
            Thread thr = new Thread(new ThreadStart(Info.server.Start));
            thr.IsBackground = true;
            thr.Start();
        }

        #region CommandsEventHandlers
        private void NotYetImplemented_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var w = new AddItem();
            w.ShowDialog();
        }

        private void cmdbtnShow_Excuted(object sender, ExecutedRoutedEventArgs e)
        {
            var w = new ShowMenu();
            w.ShowDialog();
        }

        private void EditMenu_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var w = new EditMenu();
            w.ShowDialog();
        }

        private void AddItem_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var w = new AddItem();
            w.ShowDialog();
        }

        private void ShowMusic_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var w = new ShowMusic();
            w.ShowDialog();
        }

        private void AddMusic_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var w = new AddMusic();
            w.ShowDialog();
        }

        private void AddGenre_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var w = new AddGenre();
            w.ShowDialog();
        }

        private void EditMusic_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var w = new EditMusic();
            w.ShowDialog();
        }

        private void AddCategory_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var w = new AddCategory();
            w.ShowDialog();
        }

        private void ChangeLibraryLocation_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var w = new ChangeLibraryLocation();
            w.ShowDialog();
        }

        private void cmdbtnShowAccounts_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var w = new ShowAccounts();
            w.ShowDialog();
        }

        private void cmdbtnAddAnAccount_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var w = new AddAnAccount();
            w.ShowDialog();
        }

        private void cmdbtnEditAccounts_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var w = new EditCustomers();
            w.ShowDialog();
        }

        private void cmdbtnShowFeedbacks_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var w = new ShowFeedback();
            w.ShowDialog();
        }

        private void cmdbtnShowEmployees_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var w = new ShowEmployees();
            w.ShowDialog();
        }

        private void cmdbtnAddAnEmployee_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var w = new AddAnEmployee();
            w.ShowDialog();
        }

        private void cmdbtnEditEmployees_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var w = new EditEmployees();
            w.ShowDialog();
        }

        private void cmdbtnShowOrders_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var w = new ShowOrders();
            w.ShowDialog();
        }

        private void cmdbtnAddAnOrder_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var w = new AddAnOrder();
            w.ShowDialog();
        }

        private void cmdbtnEditOrders_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var w = new EditOrder();
            w.ShowDialog();
        }

        private void cmdbtnShowReservations_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var w = new ShowReservations();
            w.ShowDialog();
        }

        private void cmdbtnAddAReservation_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var w = new AddAReservation();
            w.ShowDialog();
        }

        private void cmdbtnEditReservations_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var w = new EditReservations();
            w.ShowDialog();
        }

        private void cmdbtnShowWCQueue_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var w = new ShowWCQueue();
            w.ShowDialog();
        }

        private void cmdbtnShowChat_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            Info.showChatWindow = new ShowChat();
            Info.showChatWindow.ShowDialog();
        }

        private void cmdbtnEnterChat_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            Info.enterChatWindow = new EnterChat();
            Info.enterChatWindow.ShowDialog();
        }

        private void cmdbtnEnableDisableModules_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var w = new EnableDisableModules();
            w.ShowDialog();
        }

        private void cmdbtnChangePort_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var w = new ChangePort();
            w.ShowDialog();
        }

        private void cmdbtnShowUnreadFeedbacks_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var w = new ShowUnreadFeedbacks();
            w.ShowDialog();
        }
        #endregion

        private void cmdbtnChangeAccount_Excuted(object sender, ExecutedRoutedEventArgs e)
        {
            var w = new Login();
            w.ShowDialog();
        }

        private void cmdbtnLock_Excuted(object sender, ExecutedRoutedEventArgs e)
        {
            var sv = new ScreenSaver();
            sv.Height = cnvScreenSaver.ActualHeight;
            sv.Width = cnvScreenSaver.ActualWidth;
            cnvScreenSaver.Children.Add(sv);
        }

        private void cmdbtnExit_Excuted(object sender, ExecutedRoutedEventArgs e)
        {
            Close();
        }

        private void cmdbtnEnableDisableFeatures_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var w = new EnableDisableFeatures();
            w.ShowDialog();
        }

        private void cmdbtnShowPendingReservations_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var w = new ShowPendingReservations();
            w.ShowDialog();
        }

        private void cmdbtnShowPendingOrders_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var w = new ShowPendingOrders();
            w.ShowDialog();
        }

        private void cmdbtnShowLoggedInAccounts_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var w = new ShowLoggedInAccounts();
            w.ShowDialog();
        }

        private void cmdbtnEditNews_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var w = new News();
            w.ShowDialog();
        }

        private void cmdbtnMediaPlayer_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var w = new Windows.Music.MediaPlayer();
            w.Show();
        }

        private void cmdbtnShowMusicVotes_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var w = new MusicVotes();
            w.ShowDialog();
        }


        private void cmdbtnShowTVVotes_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var w = new TVVotes();
            w.ShowDialog();
        }

        private void RibbonCommand_Executed(object sender, ExecutedRoutedEventArgs e)
        {

        }

        private void RefreshAll_Click(object sender, RoutedEventArgs e)
        {
            //foreach (var item in Info.server.con.Values.OfType<ClientConnection>().ToList())
            //{
            //    Msg SendMsg = new Msg();
            //    SendMsg.IP = item.current.IP;
            //    SendMsg.port = item.current.Port;
            //    SendMsg.enumMethod = enumAdminTools.GetStatus;
            //    Info.server.SendingQueue.Enqueue(SendMsg);
            //}
        }
        private void AddTable_Click(object sender, RoutedEventArgs e)
        {
            Thread thr = new Thread(new ThreadStart(() =>
                {
                    try
                    {
                        if (Table.lstTables == null)
                        {
                            Table.lstTables = Table.Search();
                        }
                        Table table = new Table();
                        table.TableID = table.Add().Value;
                        Table.lstTables.Add(table);
                        Info.window.wrpLiveView.Dispatcher.Invoke(new ThreadStart(() =>
                        {
                            try
                            {
                                TableControl tableControl = new TableControl();
                                tableControl.TableNumber = table.TableID.ToString();
                                Info.window.wrpLiveView.Children.Add(tableControl);
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.Message, "Error!", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
                            }
                        }), null);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Error!", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
                    }
                }));
            thr.IsBackground = true;
            thr.Start();
        }
    }
}
